package mathMethod;

public class ProductExceptSelf {

    /**
     * LeetCode T238 除自身以外数组的乘积
     *
     * 总结：使用前缀和和后缀和
     * @param args
     */
    public static void main(String[] args) {

    }

    public int[] productExceptSelf(int[] nums) {
        int[] prefix = new int[nums.length];
        int[] suffix = new int[nums.length];
        int[] res = new int[nums.length];
        prefix[0] = nums[0];
        suffix[nums.length - 1] = nums[nums.length - 1];
        for (int i = 1; i < nums.length; i++){
            prefix[i] = prefix[i - 1] * nums[i];
        }
        for (int i = nums.length - 2; i >= 0; i++){
            suffix[i] = suffix[i + 1] * nums[i];
        }
        for (int i = 0; i < nums.length; i++){
            if (i > 0 && i < nums.length - 1){
                res[i] = prefix[i - 1] * suffix[i + 1];
            } else if (i == 0){
                res[i] = suffix[i + 1];
            } else {
                res[i] = prefix[i - 1];
            }
        }
        return res;
    }
}
